var NonFeedOrderModel = function (order, dealerObj) {
  BaseModel.call(this);
  var self = this;

  var fn = {
    initData: function (order, dealerObj) {
      var fields = ['materialFactory', 'articleOrderNo', 'articleNo', 'orderNo', 'companyOrderId', 'company', 'feedTotal', 'materialName',
        'materialCode', 'providerName', 'unit', 'materialModel', 'materialType', 'color', 'colorDescription', 'unitPrice', 'haveYltsTotal',
        'customer', 'orderDate', 'deliveryDate', 'availableCount', 'providerNo', 'materialDeptId', 'materialDeptName'
      ];
      self.setDataFromObj(order, fields, '');
      self.feedOrderRemark = '';

      var lineNo = order.lineNo;
      if (lineNo === undefined) {
        lineNo = null;
      }
      self.lineNo = lineNo;

      var feedTotal = self.getData('feedTotal', 'number');
      self.feedTotal = feedTotal;
      self.materialCode = self.materialCode.trim();
      self.canSelect = self.materialCode !== '';

      var feedTotalFu = order.feedTotalFu;
      feedTotalFu = Number(feedTotalFu);
      self.copefeedTotal = feedTotalFu;

      self.id = self.getId();
      self.tax = 13;
      self.productionUnitFeedCount = 0;

      var provider;
      var providerName = order.providerName;
      if (dealerObj === null) {
        provider = '';
      } else {
        provider = dealerObj[providerName];
        if (SysUtil.isEmpty(provider)) {
          provider = '';
        }
      }
      if (provider === '') {
        provider = {
          id: 0,
          dataId: '0-1',
          name: providerName,
          source: 1,
          isTax: true
        };
        self.otherProviderOption = _.clone(provider);
      }
      self.provider = provider;

      if (self.canSelect) {
        var actualFeedCount = feedTotal;
        var singleFeedCount = actualFeedCount / self.copefeedTotal;
        singleFeedCount = _.round(singleFeedCount, 4);
        self.singleFeedCount = singleFeedCount;
        self.actualFeedCount = actualFeedCount;
        self.workshop = null;
      }
    }
  };

  this.calCountData = function (field) {
    var copefeedTotal = this.copefeedTotal;
    if (field === 'actualFeedCount') {
      var singleFeedCount = this.singleFeedCount;
      if (singleFeedCount === undefined) {
        this.actualFeedCount = undefined;
        return;
      }
      var actualFeedCount = copefeedTotal * singleFeedCount;
      this.actualFeedCount = _.round(actualFeedCount, 4);
    } else if (field === 'singleFeedCount') {
      var actualFeedCount = this.actualFeedCount;
      if (actualFeedCount === undefined) {
        this.singleFeedCount = undefined;
        return;
      }
      var singleFeedCount = actualFeedCount / copefeedTotal;
      this.singleFeedCount = _.round(singleFeedCount, 4);
    }
  };

  this.getDetailObject = function () {
    var postData = {
      model: this.materialModel,
      providerName: this.providerName,
      materialName: this.materialName,
      notColorDesc: 1
    };

    var color = this.color.trim();
    if (color !== '') {
      postData.color = color;
    }
    return postData;
  };

  this.checkValid = function () {
    var fields = ['feedTotal', 'provider', 'singleFeedCount', 'actualFeedCount'];
    return CheckVaildUtil.checkNullField(fields, this);
  };

  this.getObject = function () {
    var postData = {
      feedCount: this.feedTotal,
      feedOrderId: this.orderNo,
      model: this.materialModel,
      haveYltsTotal: this.haveYltsTotal
    };
    var fields = ['materialName', 'materialCode', 'materialType', 'materialFactory', 'color', 'tax', 'unit', 'unitPrice',
      'colorDescription', 'singleFeedCount', 'actualFeedCount', 'feedOrderRemark'
    ];

    postData.productionUnitFeedCount = this.productionUnitFeedCount;

    this.setPostData(postData, fields);
    postData.color = postData.color.trim();
    if (postData.unitPrice === '') {
      postData.purchasePrice = 0;
    } else {
      postData.purchasePrice = postData.unitPrice;
    }

    var provider = this.provider;
    postData.providerName = provider.name;
    postData.agent = provider.id;
    postData.agentSource = provider.source;
    if (!provider.isTax) {
      postData.tax = 0;
    }

    var workshop = this.workshop;
    if (workshop !== null) {
      postData.useFactoryId = workshop.id;
      postData.useFactoryCount = this.actualFeedCount;
    }

    var lineNo = this.lineNo;
    if (lineNo !== null) {
      postData.lineNo = lineNo;
    }

    return postData;
  };

  this.getFeedMain = function () {
    var oldFeed = {};
    var fields = ['materialType', 'color', 'colorDescription', 'materialModel', 'materialFactory', 'orderNo', 'companyOrderId', 'feedTotal'];
    this.setPostData(oldFeed, fields);
    return oldFeed;
  };

  this.getClassName = function () {
    return 'NonFeedOrderModel';
  };

  fn.initData(order, dealerObj);
};
